package com.oceanstar.mapper;

import com.oceanstar.Pojo.*;
import org.apache.ibatis.annotations.*;

import java.util.List;

public interface ChaperonMapper {
    // 新增陪诊人员
    @Insert("INSERT INTO chaperoninfo (name, phone, gender, introduction) " +
            "VALUES (#{name}, #{phone}, #{gender}, #{introduction})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    void insertChaperon(ChaperonInfo chaperonInfo);

    // 删除陪诊人员
    @Delete("DELETE FROM chaperoninfo WHERE id = #{id}")
    void deleteChaperonById(Integer id);

    // 修改陪诊人员信息
    @Update("UPDATE chaperoninfo SET name=#{name}, phone=#{phone}, " +
            "gender=#{gender}, introduction=#{introduction} WHERE id=#{id}")
    void updateChaperonInfo(ChaperonInfo chaperonInfo);

    // 查询所有陪诊人员
    @Select("SELECT id, name, gender, phone, introduction FROM chaperoninfo")
    List<ChaperonInfo> getAllChaperon();

    // 根据ID查询陪诊人员
    @Select("SELECT id, name, gender, phone, introduction FROM chaperoninfo WHERE id = #{id}")
    ChaperonInfo selectChaperonById(Integer id);

    // 插入陪诊服务
    @Insert("INSERT INTO services (chaperon_id, service_date) " +
            "VALUES (#{chaperonId}, #{serviceDate})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    void insertServices(ServiceInfo serviceInfo);

    // 查询陪诊服务列表
    @Select("SELECT id, chaperon_id, service_date, is_booked FROM services")
    List<ServiceInfo> selectServicesList();

    // 查询陪诊服务总数
    @Select("SELECT COUNT(*) FROM services")
    long countServices();

    // 根据ID查询陪诊服务
    @Select("SELECT id, chaperon_id, service_date, is_booked FROM services WHERE id = #{id}")
    ServiceInfo selectServicesById(Integer id);

    // 删除陪诊服务
    @Delete("DELETE FROM services WHERE id = #{id}")
    void deleteServiceById(Integer id);

    // 根据日期查询陪诊服务
    @Select("SELECT id, chaperon_id, service_date, is_booked FROM services WHERE service_date = #{service_date}")
    List<ServiceInfo> selectServicesListByDate(String service_date);

    // 检查特定服务是否已被预约
    @Select("SELECT is_booked FROM services WHERE chaperon_id = #{chaperonId} AND DATE(service_date) = #{serviceDate}")
    int countBookingsByChaperonAndDate(@Param("chaperonId") Integer chaperonId, @Param("serviceDate") String serviceDate);

    // 插入预约信息
    @Insert("INSERT INTO servicebook (" +
            "order_id, book_time, user_id, chaperon_id, " +
            "description_of_symptoms, medical_history, allergy_history" +
            ") VALUES (" +
            "#{orderId}, #{bookTime}, #{userId}, #{chaperonId}, " +
            "#{descriptionOfSymptoms}, #{medicalHistory}, #{allergyHistory}" +
            ")")
    void insertServiceBook(ServiceBookInfo serviceBookInfo);

    // 根据ID查询预约信息
    @Select("SELECT * FROM servicebook WHERE order_id = #{orderId}")
    ServiceBookInfoDTO selectServicesBookById(String orderId);

    // 根据订单ID查询完整的预约信息（用于取消预约时获取陪诊人员ID）
    @Select("SELECT order_id, book_time, user_id, chaperon_id, description_of_symptoms, medical_history, allergy_history FROM servicebook WHERE order_id = #{orderId}")
    ServiceBookInfo selectServiceBookInfoById(String orderId);

    // 删除预约信息
    @Delete("DELETE FROM servicebook WHERE order_id = #{orderId}")
    void deleteServiceBookById(String orderId);

    // 查询所有预约信息
    @Select("SELECT * FROM servicebook")
    List<ServiceBookInfo> selectAllServiceBooks();

    // 更新服务的预约状态
    @Update("UPDATE services SET is_booked = #{isBooked} WHERE chaperon_id = #{chaperonId} AND service_date = #{serviceDate}")
    void updateServiceBookingStatus(@Param("chaperonId") Integer chaperonId, @Param("serviceDate") String serviceDate, @Param("isBooked") Boolean isBooked);

    // 检查用户是否已经预约过特定陪诊人员在特定日期的服务
    @Select("SELECT COUNT(*) FROM servicebook WHERE user_id = #{userId} AND chaperon_id = #{chaperonId} AND DATE(book_time) = #{serviceDate}")
    int countUserBookingsByChaperonAndDate(@Param("userId") Integer userId, @Param("chaperonId") Integer chaperonId, @Param("serviceDate") String serviceDate);

    // 根据陪诊人员ID和服务日期查询服务信息
    @Select("SELECT id, chaperon_id, service_date, is_booked FROM services WHERE chaperon_id = #{chaperonId} AND service_date = #{serviceDate}")
    ServiceInfo selectServiceByChaperonIdAndDate(@Param("chaperonId") Integer chaperonId, @Param("serviceDate") String serviceDate);
}
